#include <stdio.h>

//二分查找非递归实现

int binary_search(int arr[],int size,int target)
{
    int left = 0;
    int right = size - 1;
    while(left <= right)
    {
        int mid = left + (right -  left) / 2;
        if(arr[mid] == target){
            return mid;
        }
        else if(arr[mid] < target){
            left = mid + 1;
        }
        else {
            right = mid - 1;
        }
    }
    return -1;//没有找到
}

int main()
{
    int arr[] = {1, 3, 5, 7, 9, 11};
    size_t sz = sizeof(arr)/sizeof(arr[0]);
    int target = 7;
    int result = binary_search(arr,sz,target); 
    (result == -1) ? (printf("未找到目标数\n")) : (printf("目标数的下标是：%d\n",result));
    return 0;
}